import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/register',
      name: 'register',
      component: () => import('../views/login/register.vue')
    },
    {
      path: '/',
      name: 'login',
      component: () => import('../views/login/login.vue')
    }, {
      path: '/index',
      name: 'home',
      component: () => import('../views/home/index.vue'),
      children: [
        {
          path: '/index/home',
          name: 'home',
          component: () => import('../views/home/child/home.vue'),
        },
        {
          path: '/index/user',
          name: 'user',
          component: () => import('../views/home/child/user.vue'),
        },
        {
          path: '/index/menu',
          name: 'menu',
          component: () => import('../views/home/child/menu.vue'),
        },
        {
          path: '/index/role',
          name: 'role',
          component: () => import('../views/home/child/role.vue'),
        },
        {
          path: '/index/area',
          name: 'area',
          component: () => import('../views/home/child/area.vue'),
        },
        {
          path: '/index/my',
          name: 'my',
          component: () => import('../views/home/my/index.vue')
        }
      ]
    },

  ]
})

router.beforeEach((to, from, next) => {
  let token = localStorage.getItem('token')
  if (!token && to.name !== 'login') {
    next({ path: "/" })
  } else {
    next()
  }
})

export default router